/*
**  Copyright (c) 2014 Michael Liu(michael.liu.point@gmail.com).
**
**  Project: Gateway Unified Management Platform
**  File:      osdep.h
**  Author:  Michael
**  Date:    03/20/2014
**
**  Purpose:
**    operating system depend defines.
*/

#ifndef __OPERATING_SYSTEM_DEP_H__
#define __OPERATING_SYSTEM_DEP_H__

#ifdef __cplusplus
extern "C" {
#endif				/* __cplusplus */

/* Include files. */

#include "common.h"
#include "cmrc.h"
#include "list.h"
#include "dpmgr.h"
#include "eamgr.h"
#include "samgr.h"

#include <pthread.h>
#include <sys/types.h>
#include <sys/wait.h>

/* Macro constant definitions. */

#define os_dep_mutex_init(mutexPM)			pthread_mutex_init(mutexPM, NULL)
#define os_dep_mutex_lock(mutexPM)			pthread_mutex_lock(mutexPM)
#define os_dep_mutex_trylock(mutexPM)		pthread_mutex_trylock(mutexPM)
#define os_dep_mutex_unlock(mutexPM)		pthread_mutex_unlock(mutexPM)
#define os_dep_mutex_destory(mutexPM)		pthread_mutex_destroy(mutexPM)

#define os_dep_cond_init(condPM)			pthread_cond_init(condPM, NULL)
#define os_dep_cond_signal(condPM)			pthread_cond_signal(condPM)
#define os_dep_cond_wait(condPM, mutexPM)	pthread_cond_wait(condPM, mutexPM)
#define os_dep_cond_timedwait(condPM, mutexPM, timeoutPM) \
											pthread_cond_timedwait(condPM, mutexPM, timeoutPM)
#define os_dep_cond_destory(condPM)			pthread_cond_destroy(condPM)

/* Type definitions. */

	typedef pid_t tos_dep_task;

	typedef pthread_mutex_t tos_dep_mutex;
	typedef pthread_cond_t tos_dep_cond;

/* External function declarations. */

/*
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!NOTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!                                                                                                !!
!!     MUST ADD NULL for the last parameter                                       !!
!!     eg: os_dep_do_system_raw(0, NULL, "ls","/home", NULL);              !!
!!                                                                                                !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*/
	extern int os_dep_do_system_raw(unsigned char wait, tos_dep_task * child, const char *exec_file, ...);

	extern int os_dep_wait(tos_dep_task * task, int *status, int option);

	extern int os_dep_kill(tos_dep_task child, int signal);
	extern int os_dep_killall(const char *exec_file, const char *signal);

	extern e_ret os_dep_if_up(const char *if_name);
	extern e_ret os_dep_if_down(const char *if_name);

	extern e_ret os_dep_create_bridge(tdp_if * dp_if);
	extern e_ret os_dep_destroy_bridge(tdp_if * dp_if);
	extern e_ret os_dep_add_bridge_member(tdp_if * dp_if, tdp_if * member_if);
	extern e_ret os_dep_del_bridge_member(tdp_if * dp_if, tdp_if * member_if);

	extern e_bool os_dep_is_valid_ip(const char *ipaddr);
	extern e_ret os_dep_get_if_mtu(const char *name, int *mtu);
	extern e_ret os_dep_set_if_mtu(const char *name, int mtu);
	extern e_ret os_dep_get_if_macaddr(const char *name, char *macaddr);
	extern e_ret os_dep_set_if_macaddr(const char *name, const char *macaddr);
	extern e_ret os_dep_set_if_ipaddr(const char *name, const char *ipaddr);
	extern e_ret os_dep_get_if_ipaddr(const char *name, char *ipaddr);
	extern e_ret os_dep_set_if_netmask(const char *name, const char *netmask);
	extern e_ret os_dep_get_if_netmask(const char *name, char *netmask);
	extern e_ret os_dep_change_default_gw(const char *defaultgw);
	extern e_ret os_dep_get_default_gw(char *defaultgw);
	extern e_ret os_dep_get_name_server(char *dns1, char *dns2);
	extern e_ret os_dep_update_name_server(const char *dns1, const char *dns2);

	extern e_ret os_dep_add_vlan_if(tdp_if * dp_if, const char *base_if, const int vid);
	extern e_ret os_dep_del_vlan_if(tdp_if * dp_if, const char *base_if, const int vid);

	extern e_ret os_dep_get_uptime(struct date_time *datetime);
	extern e_ret os_dep_get_datetime(struct date_time *datetime);
	extern e_ret os_dep_set_datetime(struct date_time *datetime);

/* Macro API definitions. */

#define os_dep_do_system(exec_file, ...) \
	os_dep_do_system_raw(eBOOL_TRUE, NULL, exec_file, ##__VA_ARGS__)

/* Global variable declarations. */

#ifdef __cplusplus
}				/* extern "C" */
#endif				/* __cplusplus */
#endif				/* __OPERATING_SYSTEM_DEP_H__ */
